package com.example.draganddrop;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	
	View source = null;
	
	
	String[] colours = {"brickredimg", "whiteimg", "redimg", "orangeimg", "yellowimg", "greenimg"};
  
/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.brickredimg).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.whiteimg).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.redimg).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.orangeimg).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.yellowimg).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.greenimg).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.topmiddle).setOnDragListener(new MyDragListener());
    findViewById(R.id.topright).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottommiddle).setOnDragListener(new MyDragListener());
    findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());

  }
  
  public void toNextScreen(View v){
	  System.out.println("To next screen");
	  Intent i = new Intent(getApplicationContext(), SecondActivity.class);
	  
	  ArrayList<String> vals = new ArrayList<String>();
	  
	  LinearLayout squareone = (LinearLayout) findViewById(R.id.topleft);
	  vals.add((getResources().getResourceEntryName(squareone.getChildAt(0).getId())));
	  
	  LinearLayout squaretwo = (LinearLayout) findViewById(R.id.topmiddle);
	  vals.add((getResources().getResourceEntryName(squaretwo.getChildAt(0).getId())));
	  
	  LinearLayout squarethree = (LinearLayout) findViewById(R.id.topright);
	  vals.add((getResources().getResourceEntryName(squarethree.getChildAt(0).getId())));
	  
	  LinearLayout squarefour = (LinearLayout) findViewById(R.id.bottomleft);
	  vals.add((getResources().getResourceEntryName(squarefour.getChildAt(0).getId())));
	  
	  LinearLayout squarefive = (LinearLayout) findViewById(R.id.bottommiddle);
	  vals.add((getResources().getResourceEntryName(squarefive.getChildAt(0).getId())));
	  
	  LinearLayout squaresix = (LinearLayout) findViewById(R.id.bottomright);
	  vals.add((getResources().getResourceEntryName(squaresix.getChildAt(0).getId())));
	  
	  i.putExtra("order",vals);
	  startActivity(i);

  }

  private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
      } else {
        return false;
      }
    }
  }

  class MyDragListener implements OnDragListener {
    Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
    Drawable normalShape = getResources().getDrawable(R.drawable.shape);

    @Override
    public boolean onDrag(View v, DragEvent event) {
      int action = event.getAction();
      switch (action) {
      case DragEvent.ACTION_DRAG_STARTED:
    //    source = v;
        break;
      case DragEvent.ACTION_DRAG_ENTERED:
        v.setBackgroundDrawable(enterShape);
        break;
      case DragEvent.ACTION_DRAG_EXITED:
        v.setBackgroundDrawable(normalShape);
        break;
      case DragEvent.ACTION_DROP:
        // Dropped, reassign View to ViewGroup
        View view = (View) event.getLocalState();
        ViewGroup owner = (ViewGroup) view.getParent();
        owner.removeView(view);
        
        LinearLayout container = (LinearLayout) v;
  
        View squareToBeMoved = container.getChildAt(0);
        if(squareToBeMoved != null && squareToBeMoved != view){
	        container.removeView(container.getChildAt(0));
	        owner.addView(squareToBeMoved);
        }
        container.addView(view);
        view.setVisibility(View.VISIBLE);
        break;
      case DragEvent.ACTION_DRAG_ENDED:
        v.setBackgroundDrawable(normalShape);
      default:
        break;
      }
      return true;
    }
  }
} 